package com.zjs.jsp.ediorder.web;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zjs.jsp.domain.axd.AxdCancelOrderRootData;
import com.zjs.jsp.domain.config.WebHelpService;
import com.zjs.jsp.domain.jsp.Jsp_Order;
import com.zjs.jsp.domain.jsp.Jsp_Task;
import com.zjs.jsp.ediorder.service.ConfigClientService;
import com.zjs.jsp.ediorder.service.MqProducer;
import com.zjs.jsp.ediorder.service.mongodb.JspTaskTemplate;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 取消订单
 *
 * @author 李文
 * @create 2017-10-13 16:07
 **/
@RestController
public class CancelOrderApiController
{
    private static final Logger LOGGER_FACTORY = LoggerFactory.getLogger(QueryOrderApiController.class);

    private static final ObjectMapper json = new ObjectMapper();



    @Autowired
    private JspTaskTemplate j;

    @Autowired
    private MqProducer mqProducer;

    @Autowired
    private ConfigClientService service;

    @Value("${JSP.Topic}")
    private String Topic;
    @Value("${JSP.TagsQX}")
    private String TagsQX;


    @Value("${clientFlag}")
    private String clientFlag;

    @ApiOperation(value = "订单取消", notes = "取消订单")
    @ApiImplicitParam(name = "root", value = "数据", required = true, dataType = "AxdCancelOrderRootData", paramType = "body")
    @PostMapping("/order/Cancel/{uuid}")
    public String orderCancel(@PathVariable String uuid, @RequestBody AxdCancelOrderRootData root, HttpServletRequest request) {
        LOGGER_FACTORY.info(uuid + " 接受的数据是 " + root + "  " + WebHelpService.getParameterMap(request));
        String out;
        String x = cancel(root, uuid);
        if (x != null) {
            out = x;
        } else {
            out = WebHelpService.getString(200, "接受成功");
        }
        LOGGER_FACTORY.info(uuid + " 响应的数据是 " + out);
        return out;
    }

    private String cancel(AxdCancelOrderRootData root, String uuid) {
        try {
            Jsp_Task jspTask = j.findTask(clientFlag, root.getPartner_order_code());
            for (Jsp_Order j : jspTask.getOd()) {
                if (root.getPartner_order_code().equals(j.getPoc())) {
                    //派送中：40(取货) 签收：50 拒收：60
                    if (jspTask.getCzzt() > 30) {
                        return WebHelpService.getString(50009, "当前订单状态不允许取消 ");
                    }
                }
            }
            j.update(jspTask.getTk(), root.getPartner_order_code());
        } catch (Exception e) {
            LOGGER_FACTORY.error(uuid + " 取消订单 修改MG 失败 ", e);
            return WebHelpService.getString(40000, "取消失败 ");
        }
        try {
            if (mqProducer.send(uuid, Topic, TagsQX, root.toString(clientFlag), null) != null) {
                return null;
            } else {
                throw new Exception("发送MQ 响应为空");
            }
        } catch (Exception e) {
            LOGGER_FACTORY.error(uuid + " 取消订单 发送MQ  失败 ", e);
            return WebHelpService.getString(40000, "取消失败 ");
        }
    }
}
